$().ready(function(){
    //全局变量
    let page = {
        profit_start_time:'',
        profit_end_time:'',
        xdata:[],
        ydata:[],
        p:1,
        num:10
    };
    //页面初始化
    function init(){
        //获取当前日期
        page.today = getNow(1);
        //获取今日收益
        getOneProfit(page.today,setTodayProfit);
        //日期选择
        datePicker('profit_start_time','YYYY-MM-DD',page.today,function(dates){
            page.profit_start_time = dates;
        });
        datePicker('profit_end_time','YYYY-MM-DD',page.today,function(dates){
            page.profit_end_time = dates;
        });
        //获取全部收益
        getAllProfit(page.profit_start_time,page.profit_end_time,function(res){
            $('#all_profit').text(res);
        });
        //获取收益折线图
        getProfitCharts(page.profit_start_time,page.profit_end_time);
        //获取收益列表
        getProfitList(page.profit_start_time,page.profit_end_time,page.p,page.num);
    }
    init();



    //日期塞选全部收益按钮
    $('#screen_btn').on('click',function(){
        let start = $('#profit_start_time').val();
        let end = $('#profit_end_time').val();
        page.profit_start_time = start;
        page.profit_end_time = end;
        getAllProfit(page.profit_start_time,page.profit_end_time,function(res){
            $('#all_profit').text(res);
        });
        getProfitCharts(page.profit_start_time,page.profit_end_time);
        getProfitList(page.profit_start_time,page.profit_end_time,page.p,page.num);
    })
    //获取单日收益
    function getOneProfit(date,callback){
        getAjax(`${global.apiUrl}/admin_community_get_day_sy`,res=>{
            callback(res);
        },{
            date:date,
            community_id:global.community_id
        })
    }
    //设置今日收益
    function setTodayProfit(res){
        $('#today_profit').text(res);
    }
    //获取全部收益
    function getAllProfit(start,end,callback){
        let startStamp = '';
        let endStamp = '';
        if(start||end){
            startStamp = dateTotimestamp(start+' 00:00:00');
            endStamp = dateTotimestamp(end+' 23:59:59');
        }
        getAjax(`${global.apiUrl}/admin_community_get_all_sy`,res=>{
            console.log(res);
            callback(res);
        },{
            start_time:startStamp,
            end_time:endStamp,
            community_id: global.community_id
        })
    }
    //获取房收益折线图
    function getProfitCharts(start,end){
        let start_time = '';
        let end_time = '';
        if(start&&end){
            start_time = dateTotimestamp(start+' 00:00:00');
            end_time = dateTotimestamp(end+' 23:59:59');
        }
        getAjax(`${global.apiUrl}/admin_community_get_community_sy_picture`,res=>{
            console.log(res);
            page.xdata = [];
            page.ydata = [];
            let charts = setNewProfitArr(res);
            charts = charts.reverse();
            if(charts.length==0){
                $('#profit_charts').css('display','none');
                $('#no-profit').css('display','block');
            }else{
                $('#profit_charts').css('display','block');
                $('#no-profit').css('display','none');
            }
            for(let i in charts){
                page.xdata.push(charts[i].date);
                page.ydata.push(charts[i].price);
            }
            lineCharts(page.xdata,page.ydata)
        },{
            community_id:global.community_id,
            start_time:start_time,
            end_time:end_time
        })
    }
    //同一日数据统计
    function setNewProfitArr(data){
        let date_total = 0;
        let arr = [];
        for(let i=0;i<data.length;i++){
            let date = data[i].date;
            date_total = Number(data[i].price);
            for(let j=i+1;j<data.length;j++){
                if(data[i].date == data[j].date){
                    date_total += Number(data[j].price);
                    data.splice(j,1);
                    j--;
                }
            }
            let newObj = {
                date:date,
                price:date_total.toFixed(2)
            }
            arr.push(newObj);

        }
        return arr;
    }
    //设定折线图展现百分比
    function setChartsPer(xdata){
        let len = xdata.length;
        let p;
        if(len>120){
            p = 80;
        }
        if(len>90){
            p=75;
        }
        if(len>60){
            p = 66;
        }
        if(len>30){
            p = 50;
        }
        return p;
    }
    //设置折线图参数
    function lineCharts(xdata,ydata){
        let lineChart = echarts.init(document.getElementById('profit_charts'));
        let option = {
            title:{
                text:'收益折线图',
                textStyle:{
                    fontSize:14
                }
            },
            tooltip:{
                trigger:'axis'
            },
            legend:{
                data:['单日收益']
            },
            grid:{
                left:50
            },
            calculable:true,
            xAxis:[
                {
                    type:'category',
                    boundaryGap:false,
                    data:xdata
                }
            ],
            yAxis:[
                {
                    type:'value',
                    axisLabel:{
                        formatter:'{value}元'
                    }
                }
            ],
            dataZoom:[
                {
                    show:true,
                    type:'slider',
                    start:setChartsPer(xdata),
                    end:100,
                    left:'3%',
                    right:'3%',
                    maxValueSpan: 10
                }
            ],
            series:[
                {
                    name:'单日收益',
                    type:'line',
                    data:ydata,
                    itemStyle:{
                        normal: {
                            label:{
                                show:true,
                                position:'top',
                                textStyle:{
                                    fontSize:14
                                }
                            }
                        }
                    },
                    markPoint : {
                        data : [
                            {type : 'max', name: '最大值'},
                            {type : 'min', name: '最小值'}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name: '平均值'}
                        ]
                    }
                }
            ]
        }
        lineChart.setOption(option);
        $(window).resize(lineChart.resize);
    }
    //获取收益列表
    function getProfitList(start,end,p,num){
        let start_time = '';
        let end_time = '';
        if(start&&end){
            start_time = dateTotimestamp(start+' 00:00:00');
            end_time = dateTotimestamp(end+' 23:59:59');
        }
        getAjax(`${global.apiUrl}/admin_community_get_all_sy_list`,res=>{
            console.log(res);
            let html = '';
            if(res.list.length == 0){
                html = `<tr>
                            <td colspan="3" style="color:#aaa;text-align: center;font-size: 14px;">无收益记录</td>
                        </tr>`;
                $('#page').paging({
                    pageNo:1,
                    totalPage:0,
                    totalSize:0
                })
            }else{
                $('#page').paging({
                    pageNo:p,
                    totalPage:Math.ceil(res.count/num),
                    totalSize:res.count,
                    callback:function(page){
                        getProfitList(start,end,page,num);
                    }
                })

                let list = res.list;
                for(let i in list){
                    html += `
                       <tr>
                            <td>${list[i].order_id}</td>
                            <td>${list[i].date}</td>
                            <td>${list[i].price}</td>
                        </tr> 
                    `
                }

            }
            $('#profit_list').html(html);
        },{
            p:p,
            num:num,
            community_id:global.community_id,
            start_time:start_time,
            end_time:end_time
        })
    }
});
